package com.lijiayuedaren.servlet;

import com.lijiayuedaren.entity.Image;
import com.lijiayuedaren.entity.PageBean;
import com.lijiayuedaren.service.ImgNameService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "ImageServlet", urlPatterns = "/getImage")
public class ImageServlet extends BaseServlet {
    protected ImgNameService service = new ImgNameService();

    protected void getImage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        int current = Integer.parseInt(request.getParameter("current"));
        PageBean<Image> pageBean = service.getImage(current);
        String json = mapper.writeValueAsString(pageBean);
        out.print(json);
    }
   //随机获得图片
    public void getRandomImage(HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        List<Image> list = service.getRandomList();
        String json = mapper.writeValueAsString(list);
        out.print(json);
    }

    //删除指定id的图片
    public void delectImage(HttpServletRequest request, HttpServletResponse response) throws Exception {
        int id = Integer.parseInt(request.getParameter("id"));
        service.deleteImage(id);
    }


}
